package top.houry.stack;

import java.util.Stack;

/**
 * @Desc
 * @Author houruiyang
 * @Date 2022/10/24
 **/
public class ValidParenThese {

    public static void main(String[] args) {
        System.out.println(isValid("{}()"));
    }

    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (char c : s.toCharArray()) {
            if (c == '(' || c == '{' || c == '[') {
                stack.push(c);
            }

            if (c == ')') {
                if (stack.isEmpty() || stack.pop() != '(') {
                    return false;
                }
            }


            if (c == '}') {
                if (stack.isEmpty() || stack.pop() != '{') {
                    return false;
                }
            }

            if (c == ']') {
                if (stack.isEmpty() || stack.pop() != '[') {
                    return false;
                }
            }

        }
        return stack.isEmpty();

    }
}
